<?php
/*
//  Copyright (C) 2007 Max Plischke
//  Original Author: Max Plischke <plischke@gmail.com>
//  Link: http://code.google.com/p/open-club-directory/
// 
//  This file is part of the Open-Club-Directory Project.
//
//  Open-Club-Directory is free software: you can redistribute it and/or 
//  modify it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  Open-Club-Directory is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with Open-Club-Directory.
//  If not, see <http://www.gnu.org/licenses/>.
*/

require_once 'appinclude.php';
require_once 'funcinclude.php';

$form_club = $_REQUEST['club'];
$form_country = $_REQUEST['country'];
$form_district = $_REQUEST['district'];
$form_groupid = $_REQUEST['groupid'];

settype($form_club, "integer");
settype($form_country, "integer");
settype($form_district, "integer");
settype($form_groupid, "float");

function CleanName($n) {
	$n=explode(' ', strtolower(trim($n)));
	$r=array(strtolower($GLOBALS[_ClubName]), 'club');
	$n=implode(' ', array_diff($n, $r));
	return $n;
}
function CapitalizeName($name) {
	$name = strtolower($name);
	$name = join("'", array_map('ucwords', explode("'", $name)));
	$name = join("-", array_map('ucwords', explode("-", $name)));
	return $name;
}

$form_name = CapitalizeName(CleanName($_REQUEST['name']));
$form_city = CapitalizeName(trim($_REQUEST['city']));
$form_url = trim($_REQUEST['url']);

if(get_magic_quotes_gpc()) {
	$form_name = stripslashes($form_name);
	$form_city = stripslashes($form_city);
	$form_url = stripslashes($form_url);
}

$link = mysql_connect($_mysql_server, $_mysql_user, $_mysql_password) or die();
mysql_select_db($_mysql_db) or die();

if ($is_app_added==true) { 
	$member = member($user);
} else {
	mysql_close($link);
	echo '<fb:redirect url="'.$appcallbackurl.'">';
	exit;
}
if ($member!=false) {
	if ($form_club==$member[club_id] || $admin==true) {
		if (!empty($form_name) && !empty($form_city) && $form_country!=0 && $form_district!=0) {
		
			if ($form_url!='' AND stripos($form_url, 'http://')===false AND stripos($form_url, 'https://')===false) { $form_url='http://'.$form_url; }

				$query = sprintf("UPDATE clubs SET name='%s', district=%d,  country=%d, city='%s', url='%s', fb_groupid=%s, userid=%d, ts=NOW() WHERE clubs.id=%d LIMIT 1",
					mysql_real_escape_string($form_name, $link),
					$form_district,
					$form_country,
					mysql_real_escape_string($form_city, $link),
					mysql_real_escape_string($form_url, $link),
					$form_groupid,
					$user,
					$form_club);
				echo $query;
				$result = mysql_query($query) or die();
				echo '<fb:redirect url="'.$appcallbackurl.'?club='.$form_club.'">';				
		
		} else {
			$club = club($_REQUEST[club]);
			
			if (empty($form_name)) { $form_name = $club[name]; }
			if (empty($form_city)) { $form_city = $club[city]; }
			if (empty($form_country)) { $form_country = $club[country]; }
			if (empty($form_district)) { $form_district = $club[district]; }
			if (empty($form_name)) { $form_name = $club[name]; }
			// optional fields
			if (empty($_REQUEST['Save'])) {
				$form_url = $club[url];
				$form_groupid = $club[fb_groupid];
			}
			/* make sure that if an fb_group that is not on your group list is selected, is shown so that it doesn't get lost on saving the change */
			if ($form_groupid!=0) {
				$fb_group_selected = ' OR gid='.$form_groupid;
			}
			$group_arr = $facebook->api_client->fql_query("SELECT gid, name, nid, description, group_type, group_subtype, recent_news, pic, pic_big, pic_small, creator, update_time, office, website, venue FROM group WHERE gid IN (SELECT gid FROM group_member WHERE uid='$user' AND gid!='2204778486')$fb_group_selected");
			if (!empty($group_arr)) {
				foreach ($group_arr as $key => $value) {
    				$groups[$value[gid]]=$value[name];
				}
			}
			
?>


<fb:header decoration="add_border">Edit your <?php echo $_ClubName; ?> Club</fb:header>
<?php 
	if (empty($form_name) || empty($form_country) || empty($form_city) || empty($form_district)) {
		echo '<fb:error message="Missing Required Entry">';
		echo ((empty($form_name)) ? $_ClubName.' Club Name<br>' : '' );
		echo ((empty($form_country)) ? 'Country<br>' : '' );
		echo ((empty($form_city)) ? 'City<br>' : '' );
		echo ((empty($form_district)) ? 'District<br>' : '' );
		echo '</fb:error>'; 
	}
?>
<fb:editor action="editclub.php?do-it" labelwidth="150">
  <input name="club" type="hidden" value="<?php echo $_REQUEST[club]; ?>">
  <fb:editor-text label="<?php echo $_ClubName; ?> Club of" name="name" value="<?php echo $form_name; ?>"/>
  <fb:editor-custom label="Country">
    <select name="country">
      <option value="0">Select Country:</option>
      <?php echo dropdown(countries(), $form_country); ?>
    </select>
  </fb:editor-custom>
  <fb:editor-text label="City (English Name)" name="city" value="<?php echo $form_city; ?>"/>
  <fb:editor-text label="District" name="district" value="<?php echo (($form_district==0) ? '' : $form_district); ?>"/>
  <fb:editor-text label="Homepage URL" name="url" value="<?php echo $form_url; ?>"/>
  <fb:editor_custom label="Facebook Group">
    <select name="groupid">
      <option value="0">Select Club Group:</option>
      <?php echo dropdown($groups, $form_groupid); ?>
       </select>
  </fb:editor_custom>
  <fb:editor-buttonset>
    <fb:editor-button value="Save"/>
    <fb:editor-cancel href="<?php echo $appcallbackurl.'?club='.$_REQUEST[club]; ?>" />
  </fb:editor-buttonset>
</fb:editor>

<?
		}
	} else {
		//echo 'you are not allowed to edit this club!';
		echo '<fb:redirect url="'.$appcallbackurl.'">';
	}
	
} else {
	echo '<fb:redirect url="'.$appcallbackurl.'">';
}
mysql_close($link);

?>
<fb:google-analytics uacct="<?php echo $_google_analytics_account; ?>" />